<template>
  <el-dialog
    :title="title"
    :visible.sync="visibleAuth"
    width="800px"
    :close-on-click-modal="false"
  >
    <!-- 添加按钮 -->
    <div class="btn-box">
      <el-button
        type="primary"
        size="small"
        @click="showForm()"
        icon="el-icon-plus"
        >添加功能权限</el-button
      >
    </div>
    <el-table v-loading="loading" :data="authBtnList">
      <el-table-column prop="menuName" label="权限名称"></el-table-column>
      <el-table-column prop="perms" label="权限编码"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="{ row, $index }">
          <div class="btns-group">
            <el-button size="mini" @click="editAuth(row, $index)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              size="mini"
              @click="removeAuth(row, $index)"
              >删除</el-button
            >
          </div>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加弹框 -->
    <el-dialog
      title="添加权限"
      :visible.sync="addAuthShow"
      width="600px"
      :close-on-click-modal="false"
      append-to-body
    >
      <el-form
        :model="authForm"
        :rules="rules"
        ref="authForm"
        label-width="100px"
      >
        <el-form-item label="权限名称" prop="menuName">
          <el-input v-model="authForm.menuName"></el-input>
        </el-form-item>
        <el-form-item label="权限编码" prop="perms">
          <el-input v-model="authForm.perms"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelAddAuth()" size="small">取 消</el-button>
        <el-button type="primary" @click="confirmAddAuth()" size="small"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </el-dialog>
</template>

<script>
import { menuManage } from '@/api'
export default {
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: '权限'
    }
  },
  data() {
    return {
      loading: false, // 加载列表
      visibleAuth: false,
      authBtnList: [], // 权限列表
      authForm: {
        delFlag: 0,
        menuName: '',
        menuType: 2,
        parentId: '',
        perms: '',
        sortNo: 0
      },
      addAuthShow: false, // 控制显示添加权限弹框
      rules: {
        menuName: [
          { required: true, message: '此项不能为空', trigger: 'blur' }
        ],
        perms: [{ required: true, message: '此项不能为空', trigger: 'blur' }]
      },
      parentData: {} // 当前父级菜单的数据
    }
  },

  watch: {
    visible(v) {
      if (v) {
        this.visibleAuth = v
      }
    },
    visibleAuth(v) {
      if (!v) {
        this.$emit('update:visible', v)
      }
    }
  },

  methods: {
    async getAuthBtnList(data) {
      this.loading = true
      const res = await menuManage.listMenuJurisdiction(data.id)
      this.authBtnList = res.data
      this.loading = false
    },

    setData(data) {
      this.parentData = data
    },

    // 添加权限
    showForm() {
      this.addAuthShow = true
      this.authForm = {
        delFlag: 0,
        menuName: '',
        menuType: 2,
        parentId: '',
        perms: '',
        sortNo: 0
      }
    },

    // 确认添加权限
    confirmAddAuth() {
      this.$refs['authForm'].validate(valid => {
        if (!valid) return false

        const isEdit = this.authForm.id
        console.log(this.authForm)
        this.authForm.parentId = this.parentData.id
        // return false
        // 有id为编辑，没有为添加
        !isEdit
          ? menuManage.insert(this.authForm).then(res => {
              this.$message.success('添加成功')
              this.getAuthBtnList(this.parentData)
            })
          : menuManage.update(this.authForm).then(res => {
              this.$message.success('编辑成功')
              this.getAuthBtnList(this.parentData)
            })

        this.addAuthShow = false
      })
    },

    // 取消
    cancelAddAuth() {
      this.addAuthShow = false
    },

    // 编辑
    editAuth(data, index) {
      this.addAuthShow = true
      this.authForm = { ...data }
    },

    // 删除
    async removeAuth(data, index) {
      try {
        const btn = await this.$confirm('确定删除此项权限?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        await menuManage.deleteById(data.id)
        this.$message.success('已删除')
        this.getAuthBtnList(this.parentData)
      } catch {
        console.info('取消删除')
      }
    }
  },

  mounted() {}
}
</script>

<style></style>
